package com.qiongqi.datadir.service.impl;

import com.qiongqi.datadir.config.DataDirConfig;
import com.qiongqi.datadir.domain.DataProvContract;
import com.qiongqi.datadir.mapper.DataProvContractMapper;
import com.qiongqi.datadir.service.IDataProvContractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.List;

import static com.qiongqi.datadir.utils.uuid.IdUtils.fastSimpleUUID;
import static java.util.Objects.nonNull;
import static org.apache.commons.lang3.ArrayUtils.isNotEmpty;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

@Slf4j
@Service
@Transactional
public class DataProvContractServiceImpl implements IDataProvContractService {

    @Autowired
    private DataProvContractMapper dataProvContractMapper;

    @Autowired
    private DataDirConfig dataDirConfig;

    /**
     * 查询资源
     *
     * @param id 资源主键
     * @return 资源
     */
    @Override
    public DataProvContract selectDataProvContractById(String id) {
        return isNotBlank(id) ? dataProvContractMapper.selectDataProvContractById(id) : null;
    }

    /**
     * 查询资源合约
     *
     * @param resId 资源主键
     * @return 资源合约
     */
    @Override
    public DataProvContract selectDataProvContractByResId(String resId) {
        return isNotBlank(resId) ? dataProvContractMapper.selectDataProvContractByResId(resId) : null;
    }

    /**
     * 查询资源列表
     *
     * @param dataProvContract 资源
     * @return 资源集合
     */
    @Override
    public List<DataProvContract> selectDataProvContractList(DataProvContract dataProvContract) {
        if(nonNull(dataProvContract)){
            dataProvContract.setCreateBy(dataDirConfig.getName());
        }
        return nonNull(dataProvContract) ? dataProvContractMapper.selectDataProvContractList(dataProvContract) : List.of();
    }

    /**
     * 新增资源
     *
     * @param dataProvContract 资源
     * @return 结果
     */
    @Override
    public int insertDataProvContract(DataProvContract dataProvContract) {
        int ret = -1;
        if (nonNull(dataProvContract)) {
            Instant currentTime = Instant.now();
            dataProvContract.setId(fastSimpleUUID());
            dataProvContract.setCreateBy(dataDirConfig.getName());
            dataProvContract.setCreateTime(currentTime);
            dataProvContract.setUpdateBy(dataDirConfig.getName());
            dataProvContract.setUpdateTime(currentTime);
            ret = dataProvContractMapper.insertDataProvContract(dataProvContract);
        }
        return ret;
    }

    /**
     * 修改资源
     *
     * @param dataProvContract 资源
     * @return 结果
     */
    @Override
    public int updateDataProvContract(DataProvContract dataProvContract) {
        int ret = -1;
        if (nonNull(dataProvContract)) {
            dataProvContract.setUpdateBy(dataDirConfig.getName());
            dataProvContract.setUpdateTime(Instant.now());
            ret = dataProvContractMapper.updateDataProvContract(dataProvContract);
        }
        return ret;
    }

    /**
     * 批量删除资源
     *
     * @param ids 需要删除的资源主键集合
     * @return 结果
     */
    @Override
    public int deleteDataProvContractByIds(String[] ids) {
        return isNotEmpty(ids) ? dataProvContractMapper.deleteDataProvContractByIds(ids) : 0;
    }

    /**
     * 删除资源信息
     *
     * @param id 资源主键
     * @return 结果
     */
    @Override
    public int deleteDataProvContractById(String id) {
        return isNotBlank(id) ? dataProvContractMapper.deleteDataProvContractById(id) : 0;
    }
}
